//
//  LoginViewController.swift
//  weile
//
//  Created by LKM on 16/10/6.
//  Copyright © 2016年 LKM. All rights reserved.
//

import UIKit

class LoginViewController: BaseViewController {

    @IBOutlet weak var tx_phone: UITextField!
    @IBOutlet weak var tx_pass: UITextField!
    @IBOutlet weak var tx_phone_leftView: UIImageView!
    @IBOutlet weak var tx_pass_leftView: UIImageView!
    
    @IBOutlet weak var btn_login: UIButton!
    
    
    @IBOutlet weak var lbl_find_pass: UILabel!
    @IBOutlet weak var lbl_tips_reg: UILabel!
    override func viewDidLoad() {
        super.viewDidLoad()
        
        btn_login.backgroundColor = R.colors.blue;
        
        tx_phone_leftView.removeFromSuperview();
        tx_phone.leftView = tx_phone_leftView;
        tx_phone.leftViewMode = UITextFieldViewMode.always
//        tx_phone.leftView!.x += 15;
        
        tx_pass_leftView.removeFromSuperview();
        tx_pass.leftView = tx_pass_leftView
        tx_pass.leftViewMode = UITextFieldViewMode.always
        
        tx_phone.keyboardType = UIKeyboardType.phonePad;
        
        
        // Do any additional setup after loading the view.
        
        //ViewHelp.setHtmlText(lbl_tips_reg,"<font color='#666'>是否没有账户？ <font color='#333333'>注册</font>");
        
        ViewHelp.setOnClickListening(lbl_tips_reg,self, #selector(onRegTipsClick(u:)) )
        
        ViewHelp.setOnClickListening(lbl_find_pass,self, #selector(onFindPassClick(u:)) )
        
//        lbl_tips_reg.textColor
//        btn_login.titleLabel!.textColor=UIColor()
        
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
        
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
        
        let userinfo = UserInfoDao.getUserAccount();
        tx_phone.text = userinfo.username;
        tx_pass.text = userinfo.userpwd;
        
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        navigationController?.setNavigationBarHidden(true, animated: false)
    }

    //------keyboard 键盘不挡住输入框---copy http://blog.apoorvmote.com/move-uitextfield-up-when-keyboard-appears/ ----
    override func viewWillDisappear(_ animated: Bool) {
        NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: nil)
        NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: nil)
    }
    func keyboardWillShow(notification:NSNotification) {
        adjustingHeight(show: true, notification: notification)
    }
    
    func keyboardWillHide(notification:NSNotification) {
        adjustingHeight(show: false, notification: notification)
    }
    
    @IBOutlet weak var bottomConstraint: NSLayoutConstraint!
    func adjustingHeight(show:Bool, notification:NSNotification) {
//        ViewHelp.showTipsView(containView: view, tips: "ccc")
        // 1
        var userInfo = notification.userInfo!
        // 2
        let keyboardFrame:CGRect = (userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
        // 3
        let animationDurarion = userInfo[UIKeyboardAnimationDurationUserInfoKey] as! TimeInterval
        // 4
        var changeInHeight = (keyboardFrame.height-140) ;//100随意调整
        if(!show){
            changeInHeight=0;
        }
        //5
        UIView.animate(withDuration: animationDurarion, animations: { () -> Void in
//            self.bottomConstraint.constant += (keyboardFrame.height + 40) * (show ? 1 : -1)
            self.view.bounds = CGRect(x:0.0, y:changeInHeight,width:self.view.frame.size.width,height:self.view.frame.size.height);
        })
        
    }
    //------keyboard 键盘不挡住输入框--end-----



    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    var taskAlls = [Int]();
    deinit {
        //界面退出肯定就不登陆咯
        Api.destroyTask(taskSerialNumbers: taskAlls);
    }

    @IBAction func onGuestLoginClick(_ sender: AnyObject) {
        
        Api.loginGuest({(re:ResponEntity) in
            ActivityRequest.goMain();
        })
    }
    @IBAction func onLoginClick(_ sender: AnyObject) {
//        FXM.D("先跳来首页看看")
//        ActivityRequest.goMain();
        
        if ViewHelp.isTextEmpty(tx_phone) {
            ViewHelp.showTipsView(containView: self.view, tips: "请填写手机号");
            return;
        }
        if ViewHelp.isTextEmpty(tx_pass) {
            ViewHelp.showTipsView(containView: self.view, tips: "请填写密码");
            return;
        }
        
        
        ViewHelp.showProgressActivityDelay(containView: self.view)
        Api.login(username:tx_phone.text!,password :tx_pass.text!, isRetry: true) { (re:ResponEntity) in
            ViewHelp.hideProgressActivity(containView: self.view)
            if(re.isSuccess()){
                ActivityRequest.goMain();
            }else{
                ViewHelp.showTipsView(containView: self.view, tips: re.msg)
            }
        }
        taskAlls.append(Api.LoginTaskSerialNum)
//        UserInfoDao.saveUser(name: String, pass: <#T##String#>)
    }
    
    func onRegTipsClick(u :UITapGestureRecognizer){
        ActivityRequest.goRegister(self.navigationController!)
    }
    
    func onFindPassClick(u :UITapGestureRecognizer){
        ActivityRequest.goPassFind(self.navigationController!)
    }
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
